<?php

namespace App\Models;

class BidLists extends Base
{
    protected $table = 'bid_lists';

    const BID_STATUS_1 = 1;
    const BID_STATUS_2 = 2;
    const BID_STATUS_3 = 3;
    const BID_STATUS_TEXT = [
        self::BID_STATUS_1 => '<span class="el-tag">未中标</span>',
        self::BID_STATUS_2 => '<span class="el-tag el-tag--danger">已中标</span>',
        self::BID_STATUS_3 => '<span class="el-tag">投标中</span>',
    ];

    const CONTRACT_STATUS_1 = 1;
    const CONTRACT_STATUS_2 = 2;
    const CONTRACT_STATUS_TEXT = [
        self::CONTRACT_STATUS_1 => '<span class="el-tag">否</span>',
        self::CONTRACT_STATUS_2 => '<span class="el-tag el-tag--danger">是</span>',
    ];

    const PAY_STATUS_1 = 1;
    const PAY_STATUS_2 = 2;
    const PAY_STATUS_TEXT = [
        self::PAY_STATUS_1 => '<span class="el-tag">未支付</span>',
        self::PAY_STATUS_2 => '<span class="el-tag el-tag--danger">已支付</span>',
    ];

    protected $fillable = [
        'id','bid_sn','demand_id','user_id','user_name','store_id','store_name','bid_time','bid_status','finnshed_time','contract_documents_pdf',
        'contract_documents_img','contract_price','bid_fee','real_fee','contract_at','pay_end_at','contract_status','upload_at','pay_price',
        'pay_status','pay_time','order_from','created_at','updated_at'
    ];

    protected $casts = ['contract_documents_img' => 'array'];

    public function demand(){//需求
        return $this->belongsTo('App\Models\Demands', 'demand_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 20){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['bid_status']) && $params['bid_status']) {
                    $query ->where('bid_status',$params['bid_status']);
                }
                if(isset($params['demand_id']) && $params['demand_id']) {
                    $query ->where('demand_id',$params['demand_id']);
                }
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['store_id']) && $params['store_id']) {
                    $query ->where('store_id',$params['store_id']);
                }
            })->with(['demand' => function ($query) {
                $query->select(['id','title'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('budget',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'bid_status'=> self::BID_STATUS_TEXT,
            'contract_status'=> self::CONTRACT_STATUS_TEXT,
            'pay_status'=> self::PAY_STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $limit = 10, $page = null){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['kwd']) && $params['kwd']) {
                        $query ->where('title','like','%' . $params['kwd'] . '%');
                    }
                    if(isset($params['bid_status']) && $params['bid_status']) {
                        $query ->where('bid_status',$params['bid_status']);
                    }
                    if(isset($params['demand_id']) && $params['demand_id']) {
                        $query ->where('demand_id',$params['demand_id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['store_id']) && $params['store_id']) {
                        $query ->where('store_id',$params['store_id']);
                    }
                })->with(['demand' => function ($query) {
                    $query->select(['id','title','price'])->get();
                }])->when($params, function ($query) use($params){
                    if(isset($params['sort']) && $params['sort']) {
                        return $query->orderBy('budget',$params['sort']);
                    }
                })->orderBy('created_at', 'desc')->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['kwd']) && $params['kwd']) {
                        $query ->where('title','like','%' . $params['kwd'] . '%');
                    }
                    if(isset($params['bid_status']) && $params['bid_status']) {
                        $query ->where('bid_status',$params['bid_status']);
                    }
                    if(isset($params['demand_id']) && $params['demand_id']) {
                        $query ->where('demand_id',$params['demand_id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['store_id']) && $params['store_id']) {
                        $query ->where('store_id',$params['store_id']);
                    }
                })->with(['demand' => function ($query) {
                    $query->select(['id','title','price'])->get();
                }])->when($params, function ($query) use($params){
                    if(isset($params['sort']) && $params['sort']) {
                        return $query->orderBy('budget',$params['sort']);
                    }
                })->orderBy('created_at', 'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params = [], $fields = ['*']){
        return $this->select($fields)->where($params)
            ->with(['demand' => function ($query) {
                $query->select(['id','title'])->get();
            }])->first();
    }

    /**
     * 获取条数
     * @param Request $request
     * @return bool
     */
    protected function getCounts($params = []){
        $counts = $this->where(function ($query) use ($params) {
                if(isset($params['store_id']) && $params['store_id']) {
                    $query ->where('store_id',$params['store_id']);
                }
                if(isset($params['demand_id']) && $params['demand_id']) {
                    $query ->where('demand_id',$params['demand_id']);
                }
                if(isset($params['bid_status']) && $params['bid_status']) {
                    $query ->where('bid_status',$params['bid_status']);
                }
            })->count();
        return $counts;
    }

}
